<template>
  <div class="home-container">
    <div class="welcome-section">
      <h1>本地知识库问答系统</h1>
      <p class="subtitle">基于 RAG + LLM的智能问答系统</p>
      <div class="action-buttons">
        <el-button type="primary" size="large" @click="$router.push('/documents')">
          <el-icon><Document /></el-icon>
          管理文档
        </el-button>
        <el-button type="success" size="large" @click="$router.push('/chat')">
          <el-icon><ChatDotRound /></el-icon>
          开始问答
        </el-button>
      </div>
    </div>

    <div class="feature-section">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="24" :md="8" class="feature-col">
          <el-card class="feature-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><Document /></el-icon>
                <span>知识库管理</span>
              </div>
            </template>
            <div class="card-content">
              <p>支持多种文档格式</p>
              <ul>
                <li>PDF 文档</li>
                <li>Word 文档</li>
                <li>Excel 表格</li>
                <li>Markdown 文件</li>
                <li>纯文本文件</li>
              </ul>
            </div>
          </el-card>
        </el-col>
        
        <el-col :xs="24" :sm="24" :md="8" class="feature-col">
          <el-card class="feature-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><ChatDotRound /></el-icon>
                <span>智能问答</span>
              </div>
            </template>
            <div class="card-content">
              <p>基于大语言模型</p>
              <ul>
                <li>上下文理解</li>
                <li>知识检索</li>
                <li>准确引用</li>
                <li>自然对话</li>
                <li>实时响应</li>
              </ul>
            </div>
          </el-card>
        </el-col>
        
        <el-col :xs="24" :sm="24" :md="8" class="feature-col">
          <el-card class="feature-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><Setting /></el-icon>
                <span>系统特性</span>
              </div>
            </template>
            <div class="card-content">
              <p>完全本地部署</p>
              <ul>
                <li>私有部署</li>
                <li>数据安全</li>
                <li>低延迟</li>
                <li>可定制化</li>
                <li>易扩展</li>
              </ul>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div class="stats-section" v-if="documentsStore.documents.length > 0">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="24" :md="8" class="stats-col">
          <el-card class="stats-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><Files /></el-icon>
                <span>文档总数</span>
              </div>
            </template>
            <div class="stats-value">
              {{ documentsStore.documents.length }}
            </div>
          </el-card>
        </el-col>
        
        <el-col :xs="24" :sm="24" :md="8" class="stats-col">
          <el-card class="stats-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><DocumentCopy /></el-icon>
                <span>文档大小</span>
              </div>
            </template>
            <div class="stats-value">
              {{ formatSize(totalSize) }}
            </div>
          </el-card>
        </el-col>
        
        <el-col :xs="24" :sm="24" :md="8" class="stats-col">
          <el-card class="stats-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><Timer /></el-icon>
                <span>最近更新</span>
              </div>
            </template>
            <div class="stats-value">
              {{ formatTime(latestUpdate) }}
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted } from 'vue'
import {
  Document,
  ChatDotRound,
  Setting,
  Files,
  DocumentCopy,
  Timer
} from '@element-plus/icons-vue'
import { useDocumentsStore } from '@/store/documents'
import { formatSize } from '@/utils/file'
import { formatTime } from '@/utils/time'

const documentsStore = useDocumentsStore()

// 计算总文件大小
const totalSize = computed(() => {
  return documentsStore.documents.reduce((sum, doc) => sum + (doc.size || 0), 0)
})

// 获取最近更新时间
const latestUpdate = computed(() => {
  if (documentsStore.documents.length === 0) return null
  return Math.max(...documentsStore.documents.map(doc => new Date(doc.uploadTime)))
})

onMounted(async () => {
  if (documentsStore.documents.length === 0) {
    await documentsStore.fetchDocuments()
  }
})
</script>

<style lang="scss" scoped>
.home-container {
  padding: var(--el-main-padding);
  max-width: 1200px;
  margin: 0 auto;
  
  .welcome-section {
    text-align: center;
    margin-bottom: 3rem;
    padding: 2rem 1rem;
    
    h1 {
      font-size: clamp(2rem, 5vw, 2.5rem);
      color: var(--el-text-color-primary);
      margin-bottom: 1rem;
    }
    
    .subtitle {
      font-size: clamp(1rem, 3vw, 1.2rem);
      color: var(--el-text-color-secondary);
      margin-bottom: 2rem;
    }
    
    .action-buttons {
      display: flex;
      justify-content: center;
      gap: 1rem;
      flex-wrap: wrap;
      
      .el-button {
        min-width: 160px;
        
        .el-icon {
          margin-right: 0.5rem;
        }
      }
    }
  }
  
  .feature-section {
    margin-bottom: 3rem;
    
    .feature-col {
      margin-bottom: 1.5rem;
    }
    
    .feature-card {
      height: 100%;
      transition: transform 0.3s ease;
      
      &:hover {
        transform: translateY(-5px);
      }
      
      .card-header {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        
        .el-icon {
          font-size: 1.25rem;
          color: var(--el-color-primary);
        }
        
        span {
          font-size: 1.1rem;
          font-weight: 500;
        }
      }
      
      .card-content {
        p {
          font-size: 1rem;
          color: var(--el-text-color-primary);
          margin-bottom: 1rem;
        }
        
        ul {
          list-style: none;
          padding: 0;
          margin: 0;
          
          li {
            margin-bottom: 0.5rem;
            color: var(--el-text-color-regular);
            position: relative;
            padding-left: 1.5rem;
            
            &::before {
              content: "✓";
              position: absolute;
              left: 0;
              color: var(--el-color-success);
            }
          }
        }
      }
    }
  }
  
  .stats-section {
    .stats-col {
      margin-bottom: 1.5rem;
    }
    
    .stats-card {
      .card-header {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        
        .el-icon {
          font-size: 1.25rem;
          color: var(--el-color-primary);
        }
        
        span {
          font-size: 1.1rem;
          font-weight: 500;
        }
      }
      
      .stats-value {
        font-size: 1.5rem;
        font-weight: 500;
        color: var(--el-color-primary);
        text-align: center;
        padding: 1.5rem 0;
      }
    }
  }
}

@media (max-width: 768px) {
  .home-container {
    padding: 1rem;
    
    .welcome-section {
      margin-bottom: 2rem;
      
      .action-buttons {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
        
        .el-button {
          width: 100%;
        }
      }
    }
    
    .feature-section,
    .stats-section {
      .el-col {
        padding: 0 0.5rem;
      }
    }
  }
}

:root[class~="dark"] {
  .home-container {
    .feature-card,
    .stats-card {
      background-color: var(--el-bg-color);
      border-color: var(--el-border-color-light);
    }
  }
}
</style> 